package com.example.base.utils;

import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationRunner, ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {

    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    /**
     * 根据类型获取被容器管理的bean
     * @param clz 需要获取的bean类型
     * @return
     * @param <T>
     */
    public static <T> T getBean(Class<T> clz){
        return applicationContext.getBean(clz);
    }
}
